﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using POSSystem.BLL;

namespace WebUI.Controls
{
    public partial class EditLoanPhone : BaseEditControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            base.Page_Load(sender, e);

            if (!IsPostBack)
            {
                LoadBrand();
            }
        }

        private void LoadBrand()
        {
            Brands brands = Brands.LoadAllActiveBrands();
            Utilities.LoadDropDownList(ddlBrand, brands.DefaultView, Brands.ColumnNames.BrandName, Brands.ColumnNames.ID);
        }

        protected void ddlBrand_OnSelectedIndexChanged(object sender, EventArgs e)
        {
            LoadProductList();
        }

        public void LoadProductList()
        {
            if (!string.IsNullOrEmpty(ddlBrand.SelectedValue))
            {
                ddlProduct.Items.Clear();
                ddlProduct.SelectedIndex = -1;
                Products p = Products.GetIMEIActiveProductByBrandID(int.Parse(ddlBrand.SelectedValue));
                ddlProduct.DataSource = p.DefaultView;
                ddlProduct.DataTextField = Products.ColumnNames.ProductName;
                ddlProduct.DataValueField = Products.ColumnNames.ID;
                ddlProduct.DataBind();
            }
            else
            {
                ddlProduct.DataSource = null;
                ddlProduct.DataBind();
            }
        }

        public string ProductName
        {
            get
            {
                return ddlProduct.SelectedItem.Text;
            }
            set
            {
                bool found = false;
                for(int i = 0; i < ddlProduct.Items.Count; i++)
                    if (ddlProduct.Items[i].Text == value)
                    {
                        ddlProduct.SelectedIndex = i;
                        found = true;
                        break;
                    }

                if (!found)
                {
                    ddlProduct.Items.Add(new ListItem(value, value));
                    ddlProduct.SelectedValue = value;
                }
            }
        }

        public int BrandID
        {
            get 
            {
                return ddlBrand.SelectedValue != "" ? int.Parse(ddlBrand.SelectedValue) : 0;
            }
            set
            {
                if (ddlBrand.Items.Count == 0)
                    LoadBrand();
                ddlBrand.SelectedValue = value.ToString();
                LoadProductList();
            }
        }

        public string IMEI
        {
            get
            {
                return txtIMEI.Text;
            }
            set
            {
                txtIMEI.Text = value;
            }
        }
    }
}